import { isArray, isObject } from "@vue/shared";
import { createVNode, isVnode } from "./vnode";

/**
 * h('div', {a: 23})
 * h('div', {}, 'hello world')
 * h('div', {}, h('span'))
 * h('div', h('span'))
 * h('div', null, h('span'), h('span'))
 * h('div', null, 'aaa', 'bbb', 'ccc')
 * 
 * 页面渲染函数
 * @param type 标签
 * @param propsOrChildren 属性
 * @param children 子元素
 */
export function h(type, propsOrChildren, children) {
  // console.log(type, propsOrChildren, children)

  const len = arguments.length;
  if (len === 2) { // 可能是标签和属性，也可能是标签和孩子

    if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
      if (isVnode(propsOrChildren)) { // 是虚拟节点
        return createVNode(type, null, [propsOrChildren]);
      }

      return createVNode(type, propsOrChildren);
    } else {
      // 如果第二个参数，不是对象，一定是孩子
      return createVNode(type, null, propsOrChildren);
    }
  } else {
    
    if (len > 3) {
      children = Array.prototype.slice.call(arguments, 2);
    } else  if (len === 3 && isVnode(children)) {
      children = [children];
    }

    return createVNode(type, propsOrChildren, children);
  }
}